package com.dianrui.agent.activity;

import android.annotation.SuppressLint;

import android.graphics.Bitmap;

import android.os.Handler;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.dianrui.agent.Constant;
import com.dianrui.agent.R;

import butterknife.BindView;
import butterknife.OnClick;

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.act_progressbar)
    ProgressBar mProgressBar;


    @Override
    public int getContentViewId() {
        return R.layout.webview;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView() {
        webview.loadUrl(Constant.explain_url);
        WebSettings webSetting = webview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(false);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mProgressBar != null) {
                    mProgressBar.setVisibility(View.VISIBLE);
                }
                webview.setVisibility(View.VISIBLE);


            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mProgressBar.getProgress() == 100) {
                    mProgressBar.setVisibility(View.GONE);
                }
                webview.setVisibility(View.VISIBLE);


            }


            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);


            }


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                //设置进度条的最大值
                if (mProgressBar != null) {
                    mProgressBar.setProgress(newProgress);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String titles) {
                super.onReceivedTitle(view, titles);
                if (titles != null) {
                    title.setText(titles);
                }

            }
        });


    }






    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (webview != null) {
            webview.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (webview != null) {
            webview.onPause();
        }
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
                default:
                    break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.destroy();
        webview.clearHistory();
        webview.removeAllViews();
        webview.stopLoading();
        webview.getSettings().setJavaScriptEnabled(false);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                ((ViewGroup) webview.getParent()).removeView(webview);
            }
        });
    }



}
